package com.hopedove.tempdemo

import android.os.Bundle
import android.view.Gravity
import android.widget.GridLayout
import android.widget.TextView
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.google.zxing.BarcodeFormat
import com.hopedove.base.BaseActivity
import com.hopedove.tempdemo.databinding.CraftActivitySampleNoticeBinding
import com.hopedove.tempdemo.viewmodel.SampleNoticeViewModel
import com.journeyapps.barcodescanner.BarcodeEncoder

/**
 *  试样单打印
 */
class SampleNoticeActivity : BaseActivity() {
    private lateinit var binding: CraftActivitySampleNoticeBinding
    private val vm: SampleNoticeViewModel by viewModels()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.craft_activity_sample_notice)

        vm.resp.observe(this, { resp ->
            binding.data = resp
        })

        vm.noticeBill.observe(this, { bill ->
            binding.containerNoticeBill.item = bill
            bill.CLOTHTKTNO?.let { no ->
                val bitmap = try {
                    val encoder = BarcodeEncoder()
                    val bitmap = encoder.encode(no, BarcodeFormat.CODABAR, 312, 100)
                    encoder.createBitmap(bitmap)
                } catch (e: Exception) {
                    null
                }
                binding.containerNoticeBill.qrCode = bitmap
            }
        })

        vm.triangleArrangeBitmap.observe(this, { bitmap ->
            binding.layoutSampleParamTable.triangleBitmap = bitmap
        })

        vm.knitArrangeBitmap.observe(this, { bitmap ->
            binding.layoutSampleParamTable.zzplContainer.knitArrangeBitmap = bitmap
        })

        vm.knitArrangeContent.observe(this, { content ->
            binding.layoutSampleParamTable.zzplContainer.content = content
        })

        vm.loading.observe(this, { loading -> toggleWaitDialog(loading) })

        vm.load(Glide.with(this))

        buildDynamicTable(binding.layoutSampleRecordTable.dynamicTable, 5)
    }


    private fun buildDynamicTable(
        layout: GridLayout,
        rows: Int,
    ) {
        layout.apply {
            rowCount = rows + 2
            columnCount = 6
        }

        // head
        addDynamicCell(layout, "序号", 1, 1f)
        addDynamicCell(layout, "纱支全称", 1, 5f)
        addDynamicCell(layout, "实际比列", 1, 1f)
        addDynamicCell(layout, "产地", 1, 2f)
        addDynamicCell(layout, "批号", 1, 2f)
        addDynamicCell(layout, "原料加工要求", 1, 7f)

        // data
        (1..rows).forEach {
            addDynamicCell(layout, "$it", 1, 1f)
            addDynamicCell(layout, "", 1, 5f)
            addDynamicCell(layout, "", 1, 1f)
            addDynamicCell(layout, "", 1, 2f)
            addDynamicCell(layout, "", 1, 2f)
            addDynamicCell(layout, "", 1, 7f)
        }
    }

    private fun addDynamicCell(gridLayout: GridLayout, content: String, span: Int, weight: Float) {
        val widthSpec = GridLayout.spec(GridLayout.UNDEFINED, span, weight)
        val heightSpec = GridLayout.spec(GridLayout.UNDEFINED, span, weight)
        val layoutParam = GridLayout.LayoutParams(widthSpec, heightSpec)
        TextView(this).apply {
            text = content
            setBackgroundResource(R.drawable.view_border_dark_gray)
            gravity = Gravity.CENTER
        }.also { gridLayout.addView(it, layoutParam) }
    }

}